﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ArticleUnitController : Controller
    {
        Work_ArticleUnit unit = new Work_ArticleUnit();//物品规格
        Work_ArticleInfo info = new Work_ArticleInfo();//物品
        //
        // GET: /ArticleUnit/

        public ActionResult AUIndex()
        {
            ViewBag.AD = GetQuery();
            return View();
        }
        /// <summary>
        /// 转到添加界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add(int? Id)
        {
            ArticleUnitVM idvm = new ArticleUnitVM();
            ViewBag.AID = GetQueryTime();
            if (!Id.HasValue)
            {
                idvm.ID = 0;
                idvm.AID = 0;
                idvm.Unit = "";
                idvm.Remark = "";

            }
            else
            {

                Work_ArticleUnit idinfo = unit.GetCon_ArticleUnitById(Convert.ToInt32(Id));
                Mapper.DynamicMap(idinfo, idvm);

            }
            return View(idvm);
        }

        /// <summary>
        /// 获取物品列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetQueryTime()
        { 
            IList<Work_ArticleInfo> list = info.GetAllCon_Article(" AND priorID!=0");
            IList<SelectListItem> selectList =
             list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取物品列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetQuery()
        {
            SelectListItem item = new SelectListItem();
            item.Selected = true;
            item.Text = "请选择...";
            item.Value = "0";
            IList<Work_ArticleInfo> list = info.GetAllCon_Article(" AND priorID=0");
            IList<SelectListItem> selectList =
             list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
            selectList.Insert(0,item);
            return selectList;
        }
        /// <summary>
        /// 获取规格列表
        /// </summary>
        /// <param name="pVM"></param>
        /// <returns></returns>
        public ActionResult GetWork_ArticleUnitList(PageVM pVM)
        {
            string queryConditon = string.Empty;
            if (!string.IsNullOrEmpty(pVM.query)&&pVM.query!="0")
            {
                queryConditon = " inner join work_article WA on aid=WA.id where WA.priorid=" + pVM.query + "";
            }
            int groupCount = unit.GetWork_ArticleUnitCount(queryConditon);
            IList<Work_ArticleUnit> list = unit.GetAllCon_ArticleUnit(queryConditon, pVM.rp, pVM.page);
            JsonFlexiGridData flexigridObject = new JsonFlexiGridData();
            flexigridObject.page = pVM.page;
            flexigridObject.total = groupCount;
            List<string> v;
            foreach (Work_ArticleUnit ddi in list)
            {
                v = new List<string>();
                v.Add(ddi.ID.ToString());
                v.Add(info.GetCon_ArticleById(ddi.AID).ArticleName);
                v.Add(ddi.Unit);
                v.Add(ddi.Remark);
                FlexiGridRow cell = new FlexiGridRow()
                {
                    id = ddi.ID.ToString(),
                    cell = v
                };
                flexigridObject.rows.Add(cell);
            }
            return Json(flexigridObject);
        }
        /// <summary>
        /// 删除物资规格
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete(int Id)
        {
            return Json(unit.DeleteWork_ArticleUnit(Id.ToString()) > 0 ? true : false);

        }
        /// <summary>
        /// 保存物资规格
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        public ActionResult Save(ArticleUnitVM vm)
        {
            if (ModelState.IsValid)
            {
                Work_ArticleUnit info = new Work_ArticleUnit();
                Mapper.DynamicMap(vm, info);
                unit.SaveWork_ArticleUnit(info);
                return Json(true);
            }
            else
            {
                return Json(false);
            }


        }

    }
}
